<template>
  <div class="zhanghaoshezhi">
    <div class="selectheader">
      <div class="selectheaderbox">
        <div class="selectheader-tit shoushou" :class="[select==1?'selectcolor':'']" @click="tab(1)">账号信息</div>
        <div v-if="select==1" class="xiahuaxian"></div>
      </div>
      <div class="selectheaderbox">
        <div class="selectheader-tit shoushou" :class="[select==2?'selectcolor':'']" @click="tab(2)">黑名单</div>
        <div v-if="select==2" class="xiahuaxian"></div>
      </div>
    </div>
    <!-- 账号信息 -->
    <div class="zhanghaoxinxi" v-if="select==1">
      <!-- 方块1 -->
      <div class="zhanghaoxinxibox">
        <div class="zhanghaoxinxibox-left">
          <img :src="userinfo.avatar" alt />
          <div class="zhanghaoxinxibox-lright">
            <div class="zhanghaoxinxiname">{{userinfo.nickname}}</div>
            <div>{{userinfo.into}}</div>
          </div>
        </div>
        <div class="zhanghaoxinxibox-right shoushou" @click="xiugaixinxi">修改信息</div>
      </div>
      <!-- 方块2 -->
      <div class="box2">
        <div class="box2-left-left">
          <div class="box2-left">
            <img src="../assets/jibenxinxi.png" alt />
            <div>基础信息</div>
          </div>
          <div class="box2-zhong">
            <div class="box2-zdiv1">乐讯号类型:自媒体</div>
            <div class="box2-zdiv2">乐讯号ID:{{userinfo.signal}}</div>
            <div class="box2-zdiv3">
              <div style="margin-right:20px;">作者二维码：</div>
              <div  id="qrcode"></div>
            </div>
          </div>
        </div>

        <div class="box2-right shoushou" @click="downs()">点击下载</div>
      </div>
      <!-- 方块3 和 方块4-->
      <div class="box34">
        <div class="box34-left">
          <img src="../assets/denglufangshi.png" alt />
          <div>登录方式</div>
        </div>
        <div class="box34-right">绑定手机:{{userinfo.phone}}</div>
      </div>
      <!-- 方块3 和 方块4-->
      <div class="box34">
        <div class="box34-left">
          <img src="../assets/zhanghaodengji.png" alt />
          <div>注册时间</div>
        </div>
        <div class="box34-right">
          时间:
          {{userinfo.created_at}}
        </div>
      </div>
      <!-- 方块5 -->
      <div class="box5">
        <div class="box5-left">
          <img src="../assets/yunxingzhexinxi.png" alt />
          <div>运营者信息</div>
        </div>
        <div class="box5-right">
          <div>姓名：{{userinfo.name}}</div>
          <div>身份账号：{{userinfo.identity}}</div>
          <div>性别：{{userinfo.sex==1?'男':'女'}}</div>
          <div>年龄：{{userinfo.date_birth}}</div>
          <div>所在地：{{userinfo.address}}</div>
        </div>
      </div>
    </div>

    <!-- 黑名单 -->
    <div class="heimingdan" v-if="select==2">
      <div class="heimingdanitem" v-for="(item,index) in heilist" :key="index">
        <div class="heimingdanitem-left">
          <img :src="item.avatar" alt />
          <div class="heimingdanitem-lright">
            <div class="heimingdanname">{{item.nickname}}</div>
            <div>{{item.into}}</div>
          </div>
        </div>
        <div class="heimingdanitem-right shoushou" @click="yichu(item.id)">从黑名单移除</div>
      </div>

      <img v-if="heilist.length==0" class="juzhong" src="../assets/tixian/t5.png" alt />
    </div>
  </div>
</template>

<script>
import QRCode from "qrcodejs2"; // 引入qrcode
export default {
  name: "content-manage",
  data() {
    return {
      select: 1,
      userinfo: "",
      heilist: "",
       link: "" //需要生成二维码图片的字符串
    };
  },
  components: { QRCode },
  methods: {
    xiugaixinxi() {
      this.$router.push({ name: "xiugaiziliao", params: {} });
    },
    yichu(id) {
      this.$axios({
        method: "post",
        url: "media/descs/del_black",
        data: this.$qs.stringify({
          uqid: localStorage.getItem("uqid"),
          bid: id
        }),
        headers: {
          "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
        }
      }).then(response => {
        console.log(response);
        if (response.data.error == "0") {
          this.$message({
            showClose: true,
            message: "已经从黑名单移除",
            type: "success"
          });

          this.getheilist();
        } else {
          this.$message({
            showClose: true,
            message: response.data.message,
            type: "error"
          });
        }
      });
    },
    tab(index) {
      this.select = index;

      if (index == 2) {
        this.$store.state.mianbaoxie = ["乐讯号", "账号设置", "黑名单"];
        this.getheilist();
      }

      if (index == 1) {
        this.$store.state.mianbaoxie = ["乐讯号", "账号设置", "账号信息"];
        this.getmyinfo();
      }
    },

    getmyinfo() {
      this.$get(
        // method: "get",
        "media/users/info",
        {
          uqid: localStorage.getItem("uqid")
        }
      ).then(response => {
        console.log(response);
        if (response.error == "0") {
          this.userinfo = response.data;
           this.link=String(response.data.uid)

          this.getQrcode();
        } else {
          this.$message({
            showClose: true,
            message: response.message,
            type: "error"
          });
        }
      });
    },


     getQrcode() {
      document.getElementById("qrcode").innerHTML = "";
      this.$nextTick(function() {
        //生成二维码
        this.qrcode();
      });
    },
    qrcode() {
      let that = this;
      let qrcode = new QRCode("qrcode", {
        width: 60,
        height: 60, // 高度
        text: this.link // 二维码内容
        // render: 'canvas' ,   // 设置渲染方式（有两种方式 table和canvas，默认是canvas）
        // background: '#f0f',   // 背景色
        // foreground: '#ff0'    // 前景色
      });
    },
    downs() {
      // //找到canvas标签
      let myCanvas = document
        .getElementById("qrcode")
        .getElementsByTagName("canvas");
      let img = document.getElementById("qrcode").getElementsByTagName("img");
      // // //创建一个a标签节点
      let a = document.createElement("a");
      // //设置a标签的href属性（将canvas变成png图片）
      let imgURL = myCanvas[0].toDataURL("image/jpg");
      let ua = navigator.userAgent;
      if (ua.indexOf("Trident") != -1 && ua.indexOf("Windows") != -1) {
        // IE内核 并且  windows系统 情况下 才执行;
        var bstr = atob(imgURL.split(",")[1]);
        var n = bstr.length;
        var u8arr = new Uint8Array(n);
        while (n--) {
          u8arr[n] = bstr.charCodeAt(n);
        }
        var blob = new Blob([u8arr]);
        window.navigator.msSaveOrOpenBlob(blob, "下载" + "." + "png");
      } else if (ua.indexOf("Firefox") > -1) {
        //火狐兼容下载
        let blob = this.base64ToBlob(imgURL); //new Blob([content]);
        let evt = document.createEvent("HTMLEvents");
        evt.initEvent("click", true, true); //initEvent 不加后两个参数在FF下会报错  事件类型，是否冒泡，是否阻止浏览器的默认行为
        a.download = " "; //下载图片名称，如果填内容识别不到，下载为未知文件，所以我这里就不填为空
        a.href = URL.createObjectURL(blob);
        a.dispatchEvent(
          new MouseEvent("click", {
            bubbles: true,
            cancelable: true,
            view: window
          })
        ); //兼容火狐
      } else {
        //谷歌兼容下载
        img.src = myCanvas[0].toDataURL("image/jpg");
        // a.href = myCanvas[0].toDataURL('image/png').replace('image/png', 'image/octet-stream')
        a.href = img.src;
        //设置下载文件的名字
        a.download = "下载";
        //点击
        a.click();
      }
    },
    //base64转blob
    base64ToBlob(code) {
      let parts = code.split(";base64,");
      let contentType = parts[0].split(":")[1];
      let raw = window.atob(parts[1]);
      let rawLength = raw.length;
      let uInt8Array = new Uint8Array(rawLength);
      for (let i = 0; i < rawLength; ++i) {
        uInt8Array[i] = raw.charCodeAt(i);
      }
      return new Blob([uInt8Array], { type: contentType });
    },

    getheilist() {
      this.$axios({
        method: "post",
        url: "media/descs/black_list",
        data: this.$qs.stringify({
          uqid: localStorage.getItem("uqid"),
          limit: 100,
          page: 1
        }),
        headers: {
          "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
        }
      }).then(response => {
        console.log(response);
        if (response.data.error == "0") {
          this.heilist = response.data.data;
        } else {
          this.$message({
            showClose: true,
            message: response.data.message,
            type: "error"
          });
        }
      });
    }
  },
  mounted() {
    if(!localStorage.getItem('uqid')){
      this.$router.push({ path: "/login", query: {  } });
      return
    }
    this.getmyinfo();
  }
};
</script>

<style scoped>
.juzhong {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.selectcolor {
  color: #333 !important;
}
.zhanghaoshezhi {
  width: 100%;
  height: calc(100% - 118px);
  background: rgba(255, 255, 255, 1);
  opacity: 1;
  padding-left: 20px;
  padding-right: 11px;
  padding-top: 23px;
  box-sizing: border-box;
}
.selectheader {
  width: 100%;
  height: 28px;
  display: flex;
  align-items: center;
}
.selectheaderbox {
  height: 100%;
  margin-right: 21px;
}
.selectheader-tit {
  font-size: 16px;
  font-weight: 600;
  line-height: 22px;
  color: rgba(172, 172, 172, 1);
  opacity: 1;
  margin-bottom: 3px;
}
.xiahuaxian {
  width: 35px;
  height: 3px;
  background: rgba(38, 106, 255, 1);
  opacity: 1;
  margin: auto;
}
/* 账号信息 */
.zhanghaoxinxi {
}
/* 方块1 */
.zhanghaoxinxibox {
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid rgba(231, 237, 252, 1);
  height: 121px;
}
.zhanghaoxinxibox-left {
  display: flex;
  align-items: center;
}
.zhanghaoxinxibox-left > img {
  width: 80px;
  height: 80px;
  /* background: rgba(0, 0, 0, 1); */
  border-radius: 50%;
  opacity: 1;
  margin-right: 26px;
  margin-left: 47px;
}
.zhanghaoxinxibox-lright {
  font-size: 14px;
  font-weight: 400;
  line-height: 17px;
  color: rgba(48, 49, 51, 1);
  opacity: 1;
  margin-top:13px;
}
.zhanghaoxinxiname {
  font-size: 20px;
  font-weight: 550;
  line-height: 20px;
  color: rgba(48, 49, 51, 1);
  opacity: 1;
  margin-bottom: 6px;
}
.zhanghaoxinxibox-right {
  width: 103px;
  height: 38px;
  border: 1px solid rgba(172, 172, 172, 1);
  opacity: 1;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 400;
  line-height: 38px;
  color: rgba(172, 172, 172, 1);
  text-align: center;
  margin-right: 13px;
}
/* 方块2 */
.box2 {
  width: 100%;
  height: 160px;
  margin: auto;
  padding-left: 18px;
  padding-right: 13px;
  box-sizing: border-box;
  display: flex;
  justify-content: space-between;
}
.box2-left-left {
  display: flex;
}
.box2-left {
  display: flex;
  margin-top: 20px;
  margin-right: 33px;
}
.box2-left > img {
  width: 16px;
  height: 18px;
  margin-right: 9px;
}
.box2-left > div {
  font-size: 16px;
  font-weight: 600;
  line-height: 20px;
  color: rgba(48, 49, 51, 1);
}
.box2-zhong {
}
.box2-zdiv1 {
  font-size: 14px;
  font-weight: 400;
  line-height: 17px;
  color: rgba(48, 49, 51, 1);
  opacity: 1;
  margin-top: 21px;
}
.box2-zdiv2 {
  font-size: 14px;
  font-weight: 400;
  line-height: 17px;
  color: rgba(48, 49, 51, 1);
  opacity: 1;
  margin-top: 22px;
}
.box2-zdiv3 {
  display: flex;
  align-items: center;
  margin-top: 9px;
  font-size:14px;
}
.box2-zdiv3 > div {
  font-size: 14px;
  font-weight: 400;
  line-height: 17px;
  color: rgba(48, 49, 51, 1);
  margin-right: 7px;
}
.box2-zdiv3 > img {
  width: 60px;
  height: 60px;
}
.box2-right {
  margin-top: 95px;
  font-size: 15px;
  font-weight: 400;
  line-height: 21px;
  color: #266AFF;
  opacity: 1;
}
/* 方块3 和 方块4 */
.box34 {
  width: 100%;
  height: 57px;
  display: flex;
  align-items: center;
  border-top: 1px solid rgba(231, 237, 252, 1);
  padding-left: 18px;
  box-sizing: border-box;
}
.box34-left {
  display: flex;
  align-items: center;
}
.box34-left > img {
  width: 17px;
  height: 16px;
  margin-right: 8px;
}
.box34-left > div {
  font-size: 16px;
  font-weight: 600;
  line-height: 20px;
  color: rgba(48, 49, 51, 1);
  opacity: 1;
}
.box34-right {
  font-size: 14px;
  font-weight: 400;
  line-height: 17px;
  color: rgba(48, 49, 51, 1);
  opacity: 1;
  margin-left: 39px;
}
/* 方块5 */
.box5 {
  width: 100%;
  height: 210px;
  display: flex;
  border-top: 1px solid rgba(231, 237, 252, 1);
  padding-left: 18px;
  box-sizing: border-box;
}
.box5-left {
  display: flex;
  margin-top: 20px;
  margin-right: 19px;
}
.box5-left > img {
  width: 15px;
  height: 19px;
  margin-right: 10px;
}
.box5-left > div {
  font-size: 16px;
  font-weight: 600;
  line-height: 20px;
  color: rgba(48, 49, 51, 1);
  opacity: 1;
}
.box5-right > div {
  margin-top: 24px;
  font-size: 14px;
  font-weight: 400;
  line-height: 17px;
  color: rgba(48, 49, 51, 1);
  opacity: 1;
}

/* 黑名单 */
.heimingdan {
  height: calc(100% - 28px);
  width: 100%;
  margin-top: 4px;
  overflow-y: scroll;
  position: relative;
}
.heimingdan::-webkit-scrollbar {
  display: none;
}
.heimingdanitem {
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid rgba(231, 237, 252, 1);
  height: 117px;
}
.heimingdanitem-left {
  display: flex;
  align-items: center;
}
.heimingdanitem-left > img {
  width: 60px;
  height: 60px;
  background: rgba(0, 0, 0, 1);
  border-radius: 50%;
  opacity: 1;
  margin-left: 25px;
  margin-right: 22px;
}
.heimingdanitem-lright {
  font-size: 12px;
  font-weight: 400;
  line-height: 17px;
  color: rgba(48, 49, 51, 1);
  opacity: 1;
}
.heimingdanname {
  font-size: 14px;
  font-weight: 400;
  line-height: 20px;
  color: rgba(48, 49, 51, 1);
  opacity: 1;
  margin-bottom: 9px;
}
.heimingdanitem-right {
  width: 103px;
  height: 38px;
  border: 1px solid rgba(172, 172, 172, 1);
  opacity: 1;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 400;
  line-height: 38px;
  color: rgba(172, 172, 172, 1);
  text-align: center;
  margin-right: 13px;
}
</style>